package com.taoyige.ui;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;

import com.taoyige.AppManager;

/**
 * 应用程序Activity的基类
 * 
 * @author peter
 * @version 1.0
 * @created 2014-11-12
 */
public class BaseActivity extends Activity {

	// 是否允许全屏
	private boolean allowFullScreen = true;

	// 是否允许销毁
	private boolean allowDestroy = true;
	
	public static List<String> addressIdList=new ArrayList<String>();
	public static List<String> addressNameList=new ArrayList<String>();
	public static List<String> addressClassList=new ArrayList<String>();
	
	private View view;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addressIdList.add("-1");
		if(addressIdList.get(0).equals("-1")){
			addressIdList=new ArrayList<String>();
			addressIdList.add("-1");
		}
		addressNameList.add("-1");
		if(addressNameList.get(0).equals("-1")){
			addressNameList=new ArrayList<String>();
			addressNameList.add("-1");
		}
		addressClassList.add("-1");
		if(addressClassList.get(0).equals("-1")){
			addressClassList=new ArrayList<String>();
			addressClassList.add("-1");
		}
		allowFullScreen = true;
		// 添加Activity到堆栈
		AppManager.getAppManager().addActivity(this);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();

		// 结束Activity&从堆栈中移除
		AppManager.getAppManager().finishActivity(this);
	}

	public boolean isAllowFullScreen() {
		return allowFullScreen;
	}

	/**
	 * 设置是否可以全屏
	 * 
	 * @param allowFullScreen
	 */
	public void setAllowFullScreen(boolean allowFullScreen) {
		this.allowFullScreen = allowFullScreen;
	}

	public void setAllowDestroy(boolean allowDestroy) {
		this.allowDestroy = allowDestroy;
	}

	public void setAllowDestroy(boolean allowDestroy, View view) {
		this.allowDestroy = allowDestroy;
		this.view = view;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && view != null) {
			view.onKeyDown(keyCode, event);
			if (!allowDestroy) {
				return false;
			}
		}
		return super.onKeyDown(keyCode, event);
	}
}
